package it.azienda.managerworkstandard.view;

import it.azienda.framework.persistenza.DAOConnectionSingleton;
import it.azienda.framework.utilita.Utilita;
import it.azienda.framework.view.Dettaglio;
import it.azienda.managerworkstandard.modello.BeanReparto;
import java.util.ArrayList;
import javax.faces.component.html.HtmlForm;
import javax.faces.context.FacesContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class DettaglioReparto extends Dettaglio{
    
    private HtmlForm form = new HtmlForm();
    private BeanReparto beanReparto;
    
    public void setBeanReparto(BeanReparto beanReparto) {
        setObj(beanReparto);
        this.beanReparto = beanReparto;
    }
    
    public BeanReparto getBeanReparto(){
        return (BeanReparto)getObj();
    }
    
    public HtmlForm getForm() {
        form = (HtmlForm) Utilita.getSession().getAttribute("form");
        if (form == null) {
            form = new HtmlForm();
        }
        Utilita.getSession().removeAttribute("form");
        return form;
    }
    
    public void setForm(HtmlForm form) {
        setFormObject(form);
        this.form = form;
        Utilita.getSession().setAttribute("form",form);
    }
    
    public boolean controlliPrimaSalvataggio() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        beanReparto =(BeanReparto) ctx.getExternalContext().getRequestMap().get("beanReparto");
        boolean errori = false;
        
        if(beanReparto.getCodice() == null){
            String query=" select max(Reparto.codice) from BeanReparto Reparto";
            Session session = null;
            Transaction tr = null;
            try {
                session = DAOConnectionSingleton.newSession();
                tr = session.beginTransaction();
                Query q = null;
                q = session.createQuery(query);
                ArrayList listaCodice = (ArrayList) q.list();
                Integer cod=(Integer) listaCodice.get(0);
                if(cod!=null){
                    this.beanReparto.setCodice(new Integer(cod.intValue()+1));
                }else{
                    this.beanReparto.setCodice(new Integer(1));
                }
            } catch (Exception e) {
            }
        }
        return false;
    }
}
